package controllers;

import java.util.Date;
import java.util.List;

import models.CommercialOfferer;
import models.Offer;
import models.PrivateOfferer;
import models.User;
import play.data.validation.Email;
import play.data.validation.MinSize;
import play.data.validation.Phone;
import play.data.validation.Required;
import play.data.validation.URL;
import play.data.validation.Validation;
import play.libs.Codec;
import play.mvc.Controller;

public class Profile extends Controller {

	public enum View {
		ABOUT, OFFERS
	};

	public static void show(long id, View view) {
		if (view == null)
			view = View.ABOUT;
		User user = Security.getConnected();
		User profileUser = User.findById(id);

		List<Offer> offers = Offer.find("byUser", profileUser).fetch();
		render(user, profileUser, offers, view);
	}

	public static void editProfile(long id) {
		User user = Security.getConnected();
		User editUser = User.findById(id);
		if (user == editUser) {
			render(user);
		}
		show(id, View.ABOUT);
	}

	public static void savePrivate(@Required Long id,
			@Required @Email String email, @Required String firstname,
			@Required String lastname, @Required Date dateOfBirth,
			@Required String city, @Required String oldPassword,
			String newPassword, String confirmNewPassword,
			@Required String street, @Required int streetNumber,
			@Phone String telephone, @Required @MinSize(5) int zipcode) {
		User editUser = User.findById(id);
		User loggedInUser = Security.getConnected();
		if (editUser != loggedInUser)
			Profile.show(id, View.ABOUT);

		if (editUser.checkPassword(oldPassword)) {
			editUser.city = city;
			editUser.email = email;
			editUser.firstname = firstname;
			editUser.lastname = lastname;
			editUser.dateOfBirth = dateOfBirth;
			editUser.street = street;
			editUser.streetNumber = streetNumber;
			editUser.telephone = telephone;
			editUser.zipcode = zipcode;
			if (!newPassword.equals("")) {
				if ((newPassword.equals(confirmNewPassword))
						&& newPassword.length() > 5)
					editUser.passwordHash = Codec.hexMD5(newPassword);
				else {
					if (newPassword.length() < 5)
						validation
								.addError("newPassword",
										"The password needs to have at least 5 characters.");
					else
						validation
								.addError("confirmNewPassword",
										"Password is not the same as the new password entered.");
				}
			}
			

		}
		else{
			validation.addError("oldPassword", "Password was not correct!");
		}
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			editProfile(id);
		}
		editUser.save();
		Profile.show(id, View.ABOUT);
		
	}

	public static void saveCommercial(
			@Required Long id,
			@Required @Email String email,
			@Required String hotelname,
			@URL String hotellink,
			Integer stars,
			// validates email
			@Required String firstname, @Required String lastname,
			@Required Date dateOfBirth, @Required String city,
			@Required String oldPassword, String newPassword,
			String confirmNewPassword, @Required String street,
			@Required int streetNumber, @Phone String telephone,
			@Required @MinSize(5) int zipcode) {
		CommercialOfferer editUser = CommercialOfferer.findById(id);
		User loggedInUser = Security.getConnected();
		if (editUser != loggedInUser)
			Profile.show(id, View.ABOUT);

		if (editUser.checkPassword(oldPassword)) {
			editUser.city = city;
			editUser.email = email;
			editUser.hotellink = hotellink;
			editUser.hotelname = hotelname;
			editUser.stars = stars;
			editUser.firstname = firstname;
			editUser.lastname = lastname;
			editUser.dateOfBirth = dateOfBirth;
			editUser.street = street;
			editUser.streetNumber = streetNumber;
			editUser.telephone = telephone;
			editUser.zipcode = zipcode;
			if (!newPassword.equals("")) {
				if ((newPassword.equals(confirmNewPassword))
						&& newPassword.length() > 5)
					editUser.passwordHash = Codec.hexMD5(newPassword);
				else {
					if (newPassword.length() < 5)
						validation
								.addError("newPassword",
										"The password needs to have at least 5 characters.");
					else
						validation
								.addError("confirmNewPassword",
										"Password is not the same as the new password entered.");
				}
			}
			

		}
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			editProfile(id);
		}
		editUser.save();
		Profile.show(id, View.ABOUT);
	}
}
